/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * GUIServer.java
 *
 * Created on 24-ott-2011, 15.45.07
 */
package examplesocket;

import java.util.StringTokenizer;
import java.util.Vector;

/**
 *
 * @author Administrator
 */
public class GUIServer extends javax.swing.JFrame {

    /** Creates new form GUIServer */
    public GUIServer() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        txtAreaConsole = new javax.swing.JTextArea();
        txtInputConsole = new javax.swing.JTextField();
        jScrollPane2 = new javax.swing.JScrollPane();
        txtAreaLog = new javax.swing.JTextArea();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        txtAreaConsole.setBackground(new java.awt.Color(0, 0, 0));
        txtAreaConsole.setColumns(20);
        txtAreaConsole.setEditable(false);
        txtAreaConsole.setForeground(new java.awt.Color(0, 255, 0));
        txtAreaConsole.setRows(5);
        jScrollPane1.setViewportView(txtAreaConsole);

        txtInputConsole.setBackground(new java.awt.Color(0, 0, 0));
        txtInputConsole.setFont(new java.awt.Font("Monospaced", 0, 13)); // NOI18N
        txtInputConsole.setForeground(new java.awt.Color(0, 255, 0));
        txtInputConsole.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtInputConsoleActionPerformed(evt);
            }
        });
        txtInputConsole.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtInputConsoleKeyPressed(evt);
            }
        });

        txtAreaLog.setBackground(new java.awt.Color(0, 0, 0));
        txtAreaLog.setColumns(20);
        txtAreaLog.setEditable(false);
        txtAreaLog.setForeground(new java.awt.Color(0, 255, 0));
        txtAreaLog.setRows(5);
        jScrollPane2.setViewportView(txtAreaLog);

        jLabel1.setText("Console");

        jLabel2.setText("Log");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(txtInputConsole, javax.swing.GroupLayout.DEFAULT_SIZE, 688, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 300, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel1))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 370, Short.MAX_VALUE))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 15, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jScrollPane2)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 253, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(txtInputConsole, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void txtInputConsoleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtInputConsoleActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtInputConsoleActionPerformed

    private void txtInputConsoleKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtInputConsoleKeyPressed
        // TODO add your handling code here:
        if(evt.VK_ENTER == evt.getKeyCode())
        {
            
            String command;
            Vector<String> field = new Vector();
            if(txtAreaConsole.getText() == null)
                System.out.println(txtAreaConsole.getText());//txtConsole = txtInputConsole.getText();
            else
                txtConsole = txtAreaConsole.getText() + "\n>" + txtInputConsole.getText();
            command = txtInputConsole.getText();
            StringTokenizer commandParam = new StringTokenizer(command);
            while(commandParam.hasMoreTokens())
            {
                field.add(commandParam.nextToken());
            }
            if(field.size()<1)
            {
                
            }
            else if(field.get(0).compareTo("clear") == 0)
            {
                if(field.size()>1)
                {
                    if(field.get(1).compareTo("log") == 0)
                        txtAreaLog.setText("");
                    else if(field.get(1).compareTo("console") == 0)
                        txtAreaConsole.setText(">clear");
                }
                else
                {
                    txtConsole += "\nMissing paramether - \"console\" or \"log\"";
                    txtAreaConsole.setText(txtConsole);
                }
                
            }
            else if(field.get(0).compareTo("ban") == 0)
            {
                //TODO
            }
            else if(field.get(0).compareTo("kick") == 0)
            {
                //TODO
            }
            else if(field.get(0).compareTo("stop") == 0)
            {
                txtConsole += "\nServer succesfully stopped";
                txtAreaConsole.setText(txtConsole);
                server.serviceStopper();
                serverStarted=false;
            }
            else if(field.get(0).compareTo("start") == 0)
            {
                if(field.size()>1 && !serverStarted)
                {
                    server = new Server(Integer.parseInt(field.get(1)));
                    server.start();
                    txtConsole += "\nServer succesfully started on port " + field.get(1);
                    txtAreaConsole.setText(txtConsole);
                    serverStarted = true;
                }
                else if(field.size()<=1)
                {
                    txtConsole += "\nMissing paramether - \"port\"";
                    txtAreaConsole.setText(txtConsole);
                }
                else
                {
                    txtConsole+="\nAnother server has been started, stop it and try again!";
                    txtAreaConsole.setText(txtConsole);
                }
            }
            else
            {
                txtConsole += "\nCommand " + command + " Not Found";
                txtAreaConsole.setText(txtConsole);
                System.err.println("Command " + command + " Not Found");
            }
            txtInputConsole.setText("");
        }
    }//GEN-LAST:event_txtInputConsoleKeyPressed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new GUIServer().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextArea txtAreaConsole;
    private javax.swing.JTextArea txtAreaLog;
    private javax.swing.JTextField txtInputConsole;
    // End of variables declaration//GEN-END:variables
    String txtConsole;
    String txtLog;
    Server server;
    Boolean serverStarted=false;
}
